package com.malty.learnmvc.controller;

import com.malty.learnmvc.controller.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author malty
 */
@Controller
@RequestMapping("hello")
public class HelloController {
	@RequestMapping("sayHello")
	public ModelAndView sayHello(ModelAndView modelAndView) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date = dateFormat.format(new Date());
		modelAndView.addObject("nowDate", date);
		modelAndView.addObject("msg", "sayHello()");
		modelAndView.setViewName("hello");
		return modelAndView;
	}

	@RequestMapping("/string")
	@ResponseBody
	public String text() {
		return "hi, my girl";
	}

	@PostMapping("/file")
	@ResponseBody
	public String upload(MultipartFile file) {
		return "upload a file：" + file.getOriginalFilename();
	}

	@PostMapping("/bind/obj")
	@ResponseBody
	public String paramBind(@RequestBody User user) {
		return "user: " + user;
	}
}
